var cars = new Array();
var ratio_tmp = 1;

$(document).ready(function() {
	// initialize the selects
	updateCarSelect();
	updateCarReturnSelect();

	//handle the onchange-event on the selectBox
	$("#carselect").change(function(){
		showCarAndPrice();
	});
	//handle the onchange-event on the selectBox
	$("#targetCurrency").change(function(){
		showCarAndPrice();
	});

	$("#rentCar").click(function(){
		var carID = $("#carselect option:selected").val();
		$.get("IntegratedApp", {action:"rentCar", carID:carID }, function(data) {});
		// remove the entry from the select
		$("#carselect option:selected").remove();
		$("#carselect").val(1);
		// update list of rented cars
		updateCarReturnSelect();
	});

	$("#returnCar").click(function(){
		var carID = $("#carreturn option:selected").val();
		$.get("IntegratedApp", {action:"returnCar", carID:carID }, function(data) {});
		// remove the entry from the select
		$("#carreturn option:selected").remove();
		updateCarSelect();
	});
	
	$("#btnTrafficShow").click(function(){
		var trafficUrl = $("#IDTrafficUrl").val();
		$.get("IntegratedApp", {action:"showTraffic", trafficUrl:trafficUrl }, function(data) {
			$("#trafficInfo").html(data);
			$("#trafficInfo").show();
		});
	});
	
	$("#btnTrafficHide").click(function(){
			$("#trafficInfo").hide();
	});
});

function showCarAndPrice(){
	if($("#carselect").val()!="-1"){
		$.get("IntegratedApp", {action:"getRatio", targetCurrency:$("#targetCurrency option:selected").text()}, function(data) {
			ratio_tmp = Number(data);
			$("#showCarAndPrice").html($("#carselect option:selected").text()+" kostet "+(ratio_tmp*cars[$("#carselect option:selected").val()]["price"]).toFixed(2)+" "+$("#targetCurrency option:selected").text()+" pro Tag.");
		});
	}else{
		alert("Bitte w&auml;hlen Sie ein Auto aus.");
	}
}

function updateCarSelect(){
	$.get("IntegratedApp", {action: "getCars"}, function(data) {
		var line = 0;
		$("#carselect").html("<option value=\"-1\">Auto ausw&auml;hlen</option>");
		// read all cars from the txt, which are separated with ';'
		while(data.substr(0,3) != 'EOF' && line < 20){ //line<20 is just a fallback-clausel
			cars[line] = new Object();
			cars[line]["id"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			cars[line]["price"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			cars[line]["avail"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			cars[line]["name"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			if(cars[line]["avail"] == 'A'){
				$("<option value='"+cars[line]["id"]+"'>"+cars[line]["name"]+"</option>").appendTo("#carselect");
			}
			line++;
		}
	});	
}

function updateCarReturnSelect() {
	$.get("IntegratedApp", {action: "getCars"}, function(data) {
		var line = 0;
		$("#carreturn").html("<option value=\"-1\">Auto ausw&auml;hlen</option>");
		// read all cars from the txt, which are separated with ';'
		while(data.substr(0,3) != 'EOF' && line < 20){ //line<20 is just a fallback-clausel
			cars[line] = new Object();
			cars[line]["id"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			cars[line]["price"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			cars[line]["avail"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			cars[line]["name"] = data.slice(0, data.indexOf(";"));
			data = data.substr(data.indexOf(";")+1);
			if(cars[line]["avail"] == 'B'){
				$("<option value='"+cars[line]["id"]+"'>"+cars[line]["name"]+"</option>").appendTo("#carreturn");
			}
			line++;
		}
	});	
}